package arch.chameleon.busi.sample.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import arch.chameleon.busi.sample.model.SampleCpu;
import arch.chameleon.busi.sample.model.SampleCpuSpec;
import arch.chameleon.busi.sample.model.SampleInfo;
import arch.chameleon.busi.sample.service.SampleService;
import arch.chameleon.module.web.AbstractUserController;
import arch.chameleon.support.util.UUIDGenerator;

/**
 * sample控制器
 * @author George
 *
 */
public class SampleController extends AbstractUserController {
	
	/**
	 * 创建记录
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView create(HttpServletRequest request, HttpServletResponse response) 
			throws Exception{
		String strSpecId = UUIDGenerator.create20LenUUID();//生成规格ID
		
		SampleCpuSpec objSampleCpuSpec = this.bindModel(request, SampleCpuSpec.class);
		objSampleCpuSpec.setId(strSpecId);
		log.debug("sample cpu spec: {}", new Object[]{objSampleCpuSpec});
		
		SampleCpu objSampleCpu = this.bindModel(request, SampleCpu.class);
		objSampleCpu.setId(UUIDGenerator.create20LenUUID());
		objSampleCpu.setSpecId(strSpecId);
		log.debug("sample cpu: {}", new Object[]{objSampleCpu});
		
		sampleService.save(objSampleCpu, objSampleCpuSpec);
		
		return rtnSuccessView(null, true);
	}
	
	/**
	 * 读取所有记录
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView read(HttpServletRequest request, HttpServletResponse response) 
			throws Exception{ 
		List<SampleInfo> lstSampleInfo = sampleService.seletAll();
		
		Map<String, Object> mpModel = new HashMap<String, Object>();
		mpModel.put("lstSampleInfo", lstSampleInfo);
		
		return rtnSuccessView(mpModel, true);
	}
	
	/**
	 * 通过Mybatis读取所有记录
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView readViaMybatis(HttpServletRequest request, HttpServletResponse response) 
			throws Exception{ 

		List<SampleInfo> lstMybatisSampleInfo = sampleService.selectAllViaMybatis();
		
		Map<String, Object> mpModel = new HashMap<String, Object>();
		mpModel.put("lstSampleInfo", lstMybatisSampleInfo);
		
		return rtnSuccessView(mpModel, true);
	}
	
	/**
	 * 更新指定记录
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response) 
			throws Exception{
		SampleCpuSpec objSampleCpuSpec = this.bindModel(request, SampleCpuSpec.class);
		objSampleCpuSpec.setId(this.findStringBodyValue(request, "specId"));
		log.debug("sample cpu spec: {}", new Object[]{objSampleCpuSpec});
		
		SampleCpu objSampleCpu = this.bindModel(request, SampleCpu.class);
		log.debug("sample cpu: {}", new Object[]{objSampleCpu});
		
		sampleService.save(objSampleCpu, objSampleCpuSpec);	
		
		return rtnSuccessView(null, true);
	}
	
	/**
	 * 删除指定记录
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) 
			throws Exception{
		
		sampleService.remove(
				findStringBodyValue(request, "id"), 
				findStringBodyValue(request, "specId")
		);

		return rtnSuccessView(null, true);		
	}
	
	/**
	 * 上传文件
	 * @param request
	 * @param response
	 * @param name
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public ModelAndView uploadFile(HttpServletRequest request, HttpServletResponse response) throws Exception{
		MultipartFile file = ((MultipartHttpServletRequest)request).getFile("file");
		if (!file.isEmpty()) {
			byte[] bytes = file.getBytes();
			log.debug(">>>>>>>>>>>FILE LENGTH: {}", new Object[]{bytes.length});
			response.getWriter().print(String.format("upload successfully. File length: %sBytes", bytes.length));
		} else {
			log.debug(">>>>>>>>>>>EMPTY FILE");
			response.getWriter().print("empty file.");
		}
		
		return null;
	}
	
	private SampleService sampleService;

	public void setSampleService(SampleService sampleService) {
		this.sampleService = sampleService;
	}
}

